package org.easyspring.juc;

import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @description:
 * @author：hy
 * @date: 2024/10/11
 */
public class CopyOnWriteArrayListDemo {
    private static CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
    // 模拟初始化的黑名单数据
    static {
        copyOnWriteArrayList.add("ip1");
        copyOnWriteArrayList.add("ip2");
        copyOnWriteArrayList.add("ip3");
    }

    public static void main(String[] args) throws InterruptedException {
        Runnable task = new Runnable() {
            @Override
            public void run() {
              //模拟接入用时
                try {
                    Thread.sleep(new Random().nextInt(5000));
                } catch (Exception e) {}
                String currentIP = "ipAddr" +new Random().nextInt(6);
                if(copyOnWriteArrayList.contains(currentIP)){
                    System.out.println(Thread.currentThread().getName()+" IP " +currentIP+"命中黑名单,拒绝接入处理");
                    return;
                }
                System.out.println(Thread.currentThread().getName()+" IP "+currentIP+"接入处理...");
            }
        };
        new Thread(task,"请求1").start();
        new Thread(task,"请求2").start();
        new Thread(task,"请求3").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
              //模拟用时
                try {
                    Thread.sleep(new Random().nextInt(2000));
                } catch (Exception e) {}

                String newBlackIP = "ip3";
                copyOnWriteArrayList.add(newBlackIP);
                System.out.println(Thread.currentThread().getName()+"添加新的非法IP"+newBlackIP);
            }
        },"IP黑名单更新").start();
        Thread.sleep(1000000);
    }
}
